<?php
class Model_Profile extends Model_Template
{
	
	public function __construct()
	{
		parent::__construct();
		$this->className = 'Model_Table_Profile';
		

	}
	
	/////////////////////////////////////////////////////////////return profile with a profile id
	public function getProfileById($profileId)
	{
		//Select one profile query
		$sqlSelectProfileById = 'SELECT * FROM profile WHERE profile_id=?;';
	
		//Prepare statement
		$selectProfileById = Controller_Template::$db->prepareEKADBPDO($sqlSelectProfileById, $this->className, array('profile_id','tire_set_stored','creation_date','modification_date','Person_person_id','send_to_cell_phone','send_to_email'));
	
		//Execute statement
		$selectProfileById->execute(array($profileId));
	
		return $selectProfileById->fetch();
	}
	
	/////////////////////////////////////////////////////////////return profile with a person id
	public function getProfile($personId)
	{		
		//Select one profile query
		$sqlSelectProfile = 'SELECT * FROM profile WHERE Person_person_id=?;';
		
		//Prepare statement
		$selectProfile = Controller_Template::$db->prepareEKADBPDO($sqlSelectProfile, $this->className, array('profile_id','tire_set_stored','creation_date','modification_date','Person_person_id','send_to_cell_phone','send_to_email'));
		
		//Execute statement
		$selectProfile->execute(array($personId));
		
		return $selectProfile->fetch();
	}
	
	//////////////////////////////////////////////////create a profile with all selected notification
	public function createNewProfile($newP)
	{
		$personModel = new Model_Person();
		$check = $personModel->getPerson($newP[':home_phone_number']);
		
		if(empty($check))
		{
			$personModel = new Model_Person();
			$personId=$personModel->createNewPerson(array(':first_name'=>$newP[':first_name'],':last_name'=>$newP[':last_name'],':home_phone_number'=>$newP[':home_phone_number'],':email'=>$newP[':email'],':role'=>$newP[":role"],':gender'=>$newP[':gender'],':cell_phone_number'=>$newP[':cell_phone_number'],':cell_phone_carrier'=>$newP[':cell_phone_carrier']));
			
			
			
			//Create new profile query
			$sqlCreateNewProfile = 'INSERT INTO profile (tire_set_stored,creation_date,modification_date,Person_person_id,send_to_cell_phone,send_to_email) VALUES (?,?,?,?,?,?);';
		
			//Prepare statement
			$createNewProfile = Controller_Template::$db->prepareEKADBPDO($sqlCreateNewProfile, $this->className, array('profile_id','tire_set_stored','creation_date','modification_date','Person_person_id','send_to_cell_phone','send_to_email'));
		
			//Execute statement
			$createNewProfile->execute(array($newP[':tire_set_stored'],$newP[':creation_date'],$newP[':modification_date'],$personId,$newP[':send_to_cell_phone'],$newP[':send_to_email']));
			
			$lastProfileId= Controller_Template::$db->lastInsertId();
			
			$notificationModel = new Model_Notification();
			if($newP[':note1']==1)
			{
				$notificationModel->createNewNotification(array(':notification_id'=>1, ':profile_id'=>$lastProfileId));
			}
			if($newP[':note2']==1)
			{
				$notificationModel->createNewNotification(array(':notification_id'=>2, ':profile_id'=>$lastProfileId));
			}
			if($newP[':note3']==1)
			{
				$notificationModel->createNewNotification(array(':notification_id'=>3, ':profile_id'=>$lastProfileId));
			}
			
		
			if($createNewProfile->rowCount()==1)
			{
				return $personId;
			}
		}
		else
		{
			return -2;
		}
	}
	
	
	/////////////////////////////////////////////////////////Update profile with all selected notification
	public function updateProfile($targetProfile)
	{
		
		$count=0;
		$personModel = new Model_Person();
		
		$count+=$personModel->updatePerson(array(':first_name'=>$targetProfile[':first_name'],':last_name'=>$targetProfile[':last_name'],':home_phone_number'=>$targetProfile[':home_phone_number'],':email'=>$targetProfile[':email'],':role'=>$targetProfile[':role'],':gender'=>$targetProfile[':gender'],':cell_phone_number'=>$targetProfile[':cell_phone_number'],':cell_phone_carrier'=>$targetProfile[':cell_phone_carrier'],':person_id'=>$targetProfile[':person_id']));
		
		
		$notificationModel = new Model_Notification();
		$notificationModel->deleteNotificationByProfile($targetProfile[':profile_id']);
		
		if($targetProfile[':note1']==1)
		{
			$count+=$notificationModel->createNewNotification(array(':notification_id'=>1, ':profile_id'=>$targetProfile[':profile_id']));
		}
		if($targetProfile[':note2']==1)
		{
			$count+=$notificationModel->createNewNotification(array(':notification_id'=>2, ':profile_id'=>$targetProfile[':profile_id']));
		}
		if($targetProfile[':note3']==1)
		{
			$count+=$notificationModel->createNewNotification(array(':notification_id'=>3, ':profile_id'=>$targetProfile[':profile_id']));
		}
		
		
		//Update profile query
		$sqlUpdateProfile = 'UPDATE profile SET tire_set_stored=?,modification_date=?,send_to_cell_phone=?,send_to_email=? WHERE profile_id=?;';
		
		//Prepare statement
		$updateProfile = Controller_Template::$db->prepareEKADBPDO($sqlUpdateProfile, $this->className, array('profile_id','tire_set_stored','creation_date','modification_date','send_to_cell_phone','send_to_email'));
		
		//Execute statement
		$updateProfile->execute(array($targetProfile[':tire_set_stored'],$targetProfile[':modification_date'],$targetProfile[':send_to_cell_phone'],$targetProfile[':send_to_email'],$targetProfile[':profile_id']));
			
		
		$count+=$updateProfile->rowCount();
		
		return $count;
	}
}